package com.lin.musicplayer.Modality;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by crystal on 2016/7/8.
 */
public class RotateTextView extends TextView {
    private static final   String NAME_SPACE = "http://www.baidu.com/apk/res/custom";

    private static final String ATTR_ROTATE = "rotate";
    private static final int DEFAULT_VALUE_ROTATE = 0;


    private static final String ATTR_TRANSLATE_X= "translateX";
    private static final String ATTR_TRANSLATE_Y = "translateY";
    private static final float DEFAULT_VALUE_TRANSLATE_X = 0f;
    private static final float DEFAULT_VALUE_TRANSLATE_Y = 0f;

    private int rotate = DEFAULT_VALUE_ROTATE;

    private float translateX = DEFAULT_VALUE_TRANSLATE_X;
    private float translateY = DEFAULT_VALUE_TRANSLATE_Y;

    public RotateTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        rotate = attrs.getAttributeIntValue(NAME_SPACE, ATTR_ROTATE, DEFAULT_VALUE_ROTATE);//Ðý×ª¶ÈÊý

        translateX = attrs.getAttributeFloatValue(NAME_SPACE, ATTR_TRANSLATE_X, DEFAULT_VALUE_TRANSLATE_X);//»ñÈ¡ÔÚ²¼¾ÖÖÐµÄxÖáÆ«ÒÆ°Ù·Ö±È
        translateY = attrs.getAttributeFloatValue(NAME_SPACE, ATTR_TRANSLATE_Y, DEFAULT_VALUE_TRANSLATE_Y);//»ñÈ¡ÔÚ²¼¾ÖÖÐµÄyÖáÆ«ÒÆ°Ù·Ö±È
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(getMeasuredWidth()*translateX, getMeasuredHeight()*translateY);
        //Ê×ÏÈÆ«ÒÆÔÚÐý×ª£¬ÊÇÒòÎª£¬Èç¹ûÏÈÐý×ª£¬±¾Éíxy×ø±êÏµÒ²»á¸ú×ÅÐý×ª£¬Ö®ºóÔÚÆ«ÒÆ»á²»·½±ãÎÒÃÇµÄ¿ØÖÆ£¬Ò²²»Ö±¹Û
        canvas.rotate(rotate);
        super.onDraw(canvas);
    }

}
